
package org.jsystem;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class IPFromBX {
    public IPFromBX() {
        super();
    }

    private final String USER_AGENT = "Mozilla/5.0";
    private final String HOST = "10.7.0.191:4444";
    private final String Authorization = "Basic YWRtaW46MTIzNA==";
    // HTTP GET request
    public String sendGet(String url) 
    {
    	
    	String ipIs = "";
    	try{
	        URL obj = new URL(url);
	        
	        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
	
	        // optional default is GET
	        con.setRequestMethod("GET");
	
	        //add request header
	        con.setRequestProperty("Host", HOST);
	        con.setRequestProperty("User-Agent", USER_AGENT);
	        con.setRequestProperty("Authorization", Authorization);
	
	        int responseCode = con.getResponseCode();
	        System.out.println("\nSending 'GET' request to URL : " + url);
	        System.out.println("Response Code : " + responseCode);
	
	        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
	        String inputLine;
	        StringBuffer response = new StringBuffer();
	        int cnt = 0;
	        while ((inputLine = in.readLine()) != null) {
	            response.append(inputLine);
	            if (inputLine.length() > 21) {
	                if (inputLine.contains("\"local_ip\"")) {
	                    ipIs = inputLine;
	                    ipIs.replaceAll("\\s+", "");
	                    ipIs = ipIs.substring(13, ipIs.length() - 2);
	                    System.out.println(ipIs);
	                }
	            }
	        }
	        in.close();
    	}
        catch (Exception e)
        {
            // TODO: Add catch code
            e.printStackTrace();
        }  
        return ipIs;
    }
}
